package com.smu.recognize.mqtt;

import com.smu.recognize.Util.CurrentTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import javax.swing.border.MatteBorder;

@Slf4j
public class MqttPushClient {
    private MqttConfiguration mqttConfiguration = new MqttConfiguration();

    private MqttClient client;

    private static volatile MqttPushClient mqttPushClient = null;

    public static MqttPushClient getInstance(){

        if(null == mqttPushClient){
            synchronized (MqttPushClient.class){
                if (null == mqttPushClient){
                    mqttPushClient = new MqttPushClient();
                }
            }
        }
        return mqttPushClient;
    }

    private MqttPushClient(){
        connect();
    }
    private void connect(){
        try {
            client = new MqttClient(mqttConfiguration.getHost(),mqttConfiguration.getClientid(),new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(false);  // 将订阅持久
            options.setUserName(mqttConfiguration.getUsername());
            options.setPassword(mqttConfiguration.getPassword().toCharArray());
            options.setConnectionTimeout(mqttConfiguration.getTimeout());
            options.setKeepAliveInterval(mqttConfiguration.getKeepalive());
            try {
                client.setCallback(new PushCallback());
                client.connect(options);
            }catch (Exception e){
                log.error("通信异常",e);
            }
        }catch (Exception e){
            log.error("创建客户端异常",e);
        }
    }
    public void publish(String topic, String msg){
        publish(0,false,topic,msg);
    }
    public void publish(int qos, boolean retained, String topic, String msg){
        // 消息体
        MqttMessage message = new MqttMessage();
        message.setQos(qos);
        message.setRetained(retained);
        message.setPayload(msg.getBytes());
        // 主题目标 这里用来发布
        MqttTopic mqttTopic = client.getTopic(topic);
        if (null == mqttTopic){
            log.error("topic not exist");
        }
        MqttDeliveryToken token;
        try {
            token = mqttTopic.publish(message);//跟踪消息传播的进度
            token.waitForCompletion();
            log.info("数据推送服务器[{}]",new String(message.getPayload()));
        }catch (MqttPersistenceException e){
            log.error("发布异常",e);
        }catch (MqttException e){
            log.error("发布异常",e);
        }
    }

    public static void main(String args[]){
        MqttPushClient.getInstance().connect();
        MqttPushClient.getInstance().publish("daily_notification","提醒考勤测试;123");
    }

    public void morningPublish(){
        MqttPushClient.getInstance().connect();
        log.info("早上定时消息推送");
        MqttPushClient.getInstance().
                publish("daily_notification", CurrentTimeUtil.getTime()+";"+"请及时考勤");
    }
    public void afternooonPublish(){
        MqttPushClient.getInstance().connect();
        log.info("晚上定时消息推送");
        MqttPushClient.getInstance().
                publish("daily_notification", CurrentTimeUtil.getTime()+";"+"请及时考勤");
    }
    public void testPublish(){
        MqttPushClient.getInstance().connect();
        log.info("测试消息推送");
        MqttPushClient.getInstance().
                publish("daily_notification", CurrentTimeUtil.getTime()+";"+"请及时考勤");
    }
    public void webPublish(String title,String content){
        MqttPushClient.getInstance().connect();
        log.info("Web消息推送");
        MqttPushClient.getInstance().
                publish("daily_notification", title+";"+content);
    }
    public void workPublish(String name){
        MqttPushClient.getInstance().connect();
        log.info("加班申请推送");
        MqttPushClient.getInstance().
                publish("daily_notification_mobile", name+"的新申请"+";"+"请及时审批");
    }
}
